package interfazeak;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;

import db.BezeroKud;
import db.BiltegiaKud;
import db.EnboteilaratuKud;

import Imagenes.IrudiakBiltegia;
import Taulak.MTBBiltegia;
import Taulak.MTBUpela;

public class Biltegia extends JFrame {

	private JPanel contentPane;
	private JButton btnHasieraOrria;
	private JTable table;
	private MTBBiltegia biltegitaula;
	private JButton saldubtn;
	private JButton saldubezeroaribtn;
	private JButton bezeroakikusibtn;
	private JButton ezabatu;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Biltegia frame = new Biltegia();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Biltegia() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 550);
		contentPane = new JPanel();
		setTitle("BILTEGIA");
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		IrudiakBiltegia p = new IrudiakBiltegia();
		p.setBorder(new EmptyBorder(5, 5, 5, 5));
		p.setLayout(new BorderLayout(0, 0));
		setContentPane(p);
		p.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(20,80, 560, 250);
		p.add(scrollPane);

		table = new JTable();
		biltegitaula = new MTBBiltegia();
		table.setModel(biltegitaula);
	
		scrollPane.setViewportView(table);
		
		btnHasieraOrria = new JButton("HASIERA ORRIA");
		btnHasieraOrria.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				Biltegia.this.buelta();
			}
		});
		btnHasieraOrria.setBounds(500, 20, 157, 25);
		p.add(btnHasieraOrria);
		
		
		ezabatu = new JButton("EZABATU");
		ezabatu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (table.getSelectedRow() == -1) {
					JOptionPane.showMessageDialog(Biltegia.this,
							"Lerro bat aukeratu", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);
					
				} else {
					int konfirmatu = JOptionPane.showConfirmDialog(
							Biltegia.this, "Ezabatu nahi duzu?");

					if (JOptionPane.OK_OPTION == konfirmatu) {
						Biltegia.this.ezabatu();
					}
				}

			}

		});
		ezabatu.setBounds(590,250,170,25);
		p.add(ezabatu);
		
		saldubtn= new JButton("SALDU");
		saldubtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				if (table.getSelectedRow() == -1) {
					JOptionPane.showMessageDialog(Biltegia.this,
							"Lerro bat aukeratu", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);
				}
				else {
					
				Biltegia.this.Saldu();
				}
			}
		});
		saldubtn.setBounds(590,150,170,25);
		p.add(saldubtn);
		

		saldubezeroaribtn= new JButton("BEZEROARI SALDU");
		saldubezeroaribtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				if (table.getSelectedRow() == -1) {
					JOptionPane.showMessageDialog(Biltegia.this,
							"Lerro bat aukeratu", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);
				}
				else {
					Biltegia.this.BezeroariSaldu();
				}
				
			}
		});
		saldubezeroaribtn.setBounds(590,200,170,25);
		p.add(saldubezeroaribtn);
		
		bezeroakikusibtn= new JButton("BEZEROAK IKUSI");
		bezeroakikusibtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Biltegia.this.bezeroak();
			}
		});
		bezeroakikusibtn.setBounds(220,400,190,25);
		p.add(bezeroakikusibtn);
		
		
		
		kargatu();
	}

	protected void ezabatu() {
		
		int lerroa = table.getSelectedRow();
		String data = (String) table.getValueAt(lerroa, 3);
		int ji = (int) table.getValueAt(lerroa, 4);
		int id = EnboteilaratuKud.lortuidEnboteilaratu(data, ji);
		EnboteilaratuKud.ezabatuBiltegitik(id);
		biltegitaula.ezabatu(lerroa);
	}

	protected void Saldu() {
		
		int lerroa = table.getSelectedRow();
		int kutxak = (int) table.getValueAt(lerroa, 0);
		int upela = (int) table.getValueAt(lerroa, 2);
		String data =   (String) table.getValueAt(lerroa, 3);
		int id = EnboteilaratuKud.lortuIdEnboteilaratu(kutxak,upela,data);
	
		JDialog dialog = new Saldu(Biltegia.this, true,id);
		dialog.setVisible(true);
	}

	protected void BezeroariSaldu() {
		
		int lerroa = table.getSelectedRow();
		int kutxak = (int) table.getValueAt(lerroa, 0);
		int upela = (int) table.getValueAt(lerroa, 2);
		String data =   (String) table.getValueAt(lerroa, 3);
		int id = EnboteilaratuKud.lortuIdEnboteilaratu(kutxak,upela,data);
	
		JDialog dialog = new BezeroSaldu(Biltegia.this, true,id);
		dialog.setVisible(true);
	}

	protected void bezeroak() {
		
		Bezeroa b = new Bezeroa(Biltegia.this);
		b.setVisible(true);
		this.dispose();
	}

	public void kargatu() {
		
		ResultSet rs= EnboteilaratuKud.bistaratuEnboteilaratu();
		try {
			while (rs.next()) {
				int kutxak = rs.getInt("kutxak");
				String data = rs.getString("data");
				int upela = rs.getInt("upela");
				int botilak = rs.getInt("botilak");
				int ji = rs.getInt("jatorrihasi");
				int jibuk=rs.getInt("jatorribuk");
				biltegitaula.kargatu(kutxak,data,upela,botilak,ji,jibuk);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		}
	}

	protected void buelta() {
		
		Hasiera h = new Hasiera();
		h.setVisible(true);
		this.dispose();
	}

	public void ezabatutaula() {
		int ler = table.getRowCount();	
		
		biltegitaula.taulaHasieratu(ler);
	
	}
}
